﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MainMenu
{
    public partial class frm_Main : Form
    {
        private const int _W = 120;
        private const int _H = 80;

        private List<Tuple<int, string, string>> _DATA = new List<Tuple<int, string, string>>();
        private List<Button> _CTRL = new List<Button>();
        private List<Tuple<System.Diagnostics.Process, Button>> _P = new List<Tuple<System.Diagnostics.Process, Button>>();

        public frm_Main()
        {
            InitializeComponent();
        }

        private void frm_Main_Load(object sender, EventArgs e)
        {
            this.READ_DIRS();
            this.REFR_TILES();
        }

        private void READ_DIRS()
        {
            this._P.Clear();
            this._DATA.Clear();
            string _PATH = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\..";

            foreach (string folder in System.IO.Directory.GetDirectories(_PATH))
            {
                if (System.IO.File.Exists(folder + "\\MM.INFO"))
                {
                    string[] _lines = System.IO.File.ReadAllLines(folder + "\\MM.INFO");
                    for (int i = 0; i < _lines.Length; i+=3)
                        if (_lines.Length >= i + 3)
                            _DATA.Add(new Tuple<int, string, string>(Convert.ToInt32(_lines[i]), _lines[i+1], _lines[i+2]));
                }
            }

            _DATA.Add(new Tuple<int, string, string>(Int32.MaxValue, "Einstellungen", "<DUMMY>"));

            this._DATA.Sort((x, y) => x.Item1.CompareTo(y.Item1));
        }

        private void REFR_TILES()
        {
            for(int i=0; i<this._CTRL.Count; i++)
                this.Controls.Remove(this._CTRL[i]);

            this._CTRL.Clear();

            int x = 10;
            int y = 10;

            int W = _W + 38;
            int H = _H + 120;

            for (int i = 0; i < this._DATA.Count; i++)
            {
                if (x >= 2 * _W)
                {
                    x = 10;
                    y += _H + 10;
                    H += _H + 10;
                }

                Button _b = new Button();
                _b.Text = this._DATA[i].Item2;
                _b.Name = this._DATA[i].Item3;
                _b.Size = new Size(_W, _H);
                _b.Location = new Point(x, y);
                _b.Font = new System.Drawing.Font("Arial", 10, FontStyle.Bold);

                _b.Click += new System.EventHandler(this._cmd_dyn_Click);
                this.Controls.Add(_b);

                if (i == 1)
                    W += _W + 10;

                x += _W + 10;
            }

            this.Size = new Size(W, H);
        }

        private void _cmd_dyn_Click(object sender, EventArgs e)
        {
            if (((Button)sender).Name == "<DUMMY>")
            {
                frm_DienstCFG _SCFG = new frm_DienstCFG();
                _SCFG.ShowDialog();

                return;
            }

            System.Diagnostics.Process _P = new System.Diagnostics.Process();
            _P.StartInfo.FileName = ((Button)sender).Name;
            _P.StartInfo.Arguments = Environment.CommandLine;
            this._P.Add(new Tuple<System.Diagnostics.Process, Button>(_P, (Button)sender));
            _P.Start();

            ((Button)sender).Enabled = false;
        }

        private void _cmd_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frm_Main_Shown(object sender, EventArgs e)
        {
        }

        private void _t_pw_Tick(object sender, EventArgs e)
        {
            for(int i=0; i<this._P.Count; i++)
                if(this._P[i].Item1.HasExited)
                {
                    this._P[i].Item2.Enabled = true;
                    this._P.RemoveAt(i);

                    break;
                }
        }
    }
}
